<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\Base;
use think\Model;
class Role extends Common
{
    public function _initialize()
    {
        parent::_initialize();
    }
    /*
     * 展示页面列表的方法
     *
     * */
    public function show(){
        // 查询状态为0的用户数据 并且每页显示10条数据
        $list = Db::table('role')->where('status=0 or 1')->paginate(10);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /*
    * 添加节点方法
    *
    *
    * */
    public function add()
    {
        $rolename = I('rolename');
        if (empty($rolename)) {
            $rs=Db::table('node')->select();
            $this->assign('rs',$rs);
            return $this->fetch('add');
        }else{
            $rolename = I('rolename');
            $access=$_POST['access'];
            //将数组分割为字符串
            $str=implode(".",$access);
            //再把分割好的字符串再分割为数组
            $arr=explode(".",$str);
            //分别循环取其奇数位值和偶数位值
            $zrr = array();
            for ($i = 1; $i < count($arr); $i=$i+2) {
                $zrr[] = $arr[$i];
            }
            $str=implode(',',$zrr);
            $brr =array();
            foreach($arr as $key => $value){
                if($key%2 == 0){
                    $brr[]=$value;
                }
            }
            //序列化id
            $result=json_encode($brr);
            $sort=I('sort');
            $data=array(
                'access'=>$result,
                'role_name'=>$rolename,
                'status'=>$sort,
                'access_name'=>$str,
            );
            $rs=Db::table('role')->insert($data);
            if($rs){
                $this->redirect(url($this->controller.'/show'));
            }else{
                $this->error('添加失败');
            }
        }
    }


    /*
     *
     * 删除方法，改变状态逻辑删除
     *
     * */
    public function del(){
        $id=isset($_POST['id'])?$_POST['id']:"";
        $data = ['status' => 2];
        $del=new Base();
        $del=$del->status('role',$id,$data);
        echo $del;
    }

    /*
     * 修改节点方法
     *查询node（节点表）进行修改
     * */
    public function update(){
        $rolename = I('rolename');
        if (empty($rolename)) {
            $id = I('id');
            $rs = Db::table('role')->where("id=$id")->find();
            $result=Db::table('node')->select();
           /* $rs=$rs['access_name'];
            $qq=explode(",",$rs);*/
            $this->assign('result',$result);
            $this->assign('role', $rs);
            return $this->fetch('update');
        }else{
            $id = I('id');
            $sort=I('sort');
            $checks=$_POST['access'];
            //将数组分割为字符串
            $str=implode(".",$checks);
            $arr=explode(".",$str);
            $zrr = array();
            for ($i = 1; $i < count($arr); $i=$i+2) {
                $zrr[] = $arr[$i];
            }
            $str=implode(',',$zrr);
            $brr =array();
            foreach($arr as $key => $value){
                if($key%2 == 0){
                    $brr[]=$value;
                }
            }
            $result=json_encode($brr);
            $data=array(
                'access'=>$result,
                'access_name'=>$str,
                'status'=>$sort,

            );
            $rs =Db::table('role')->where('id='.$id)->update($data);
            if ($rs) {
                //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                $this->redirect( url($this->controller.'/show'));
            } else {       //错误页面的默认跳转页面是返回前一页，通常不需要设置
                $this->error('修改角色失败');
            }
        }
    }

}